package com.dx.mes.service.tool.service;


import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.transcoder.util.*;
import com.dx.mes.facade.tool.service.IMtptoolinsService;
import com.dx.mes.facade.tool.tx.Mtptoolins.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;


@Service("mtptoolinsService")
public class MtptoolinsService implements IMtptoolinsService {
	private LogUtils logUtils;
	private static String TX_NAME = T_MTPTOOLINS;
	private static String[] Digit = { "yyyy-MM-dd 00:00:00","yyyy-MM-dd 23:59:59"};

	@Autowired
	private IBisToolDao bisToolDao;
	@Autowired
	private IRetToolInsTaskDao retToolInsTaskDao;
	@Autowired
	private IRetToolTaskDao retToolTaskDao;
	@Autowired
	private IRetToolInsItemDao retToolInsItemDao;

	@Autowired
	private IRetToolInsListDao retToolInsListDao;
	@Autowired
	private IRetToolStDao retToolStDao;
	@Autowired
	private IBisInsItemDao bisInsItemDao;

	@Override
	public String subMainProc(String evt_no, String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(MtptoolinsService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(MtptoolinsService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");

		Session session = retToolInsTaskDao.getCrSession();
		long rtnCode = _NORMAL;
		String outMsg = null;

		MtptoolinsI inTrx = new MtptoolinsI();
		MtptoolinsO outTrx = new MtptoolinsO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);


		try {
			inTrx = JacksonUtil.fromJson(strInMsg, MtptoolinsI.class);

			rtnCode = subMainProc2(inTrx, outTrx);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				session.flush();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	private long subMainProc2(MtptoolinsI inTrx, MtptoolinsO outTrx) {
		long lRc = _NORMAL;
		char actionType = inTrx.getAction_flg().toCharArray()[0];
		switch (actionType) {
			case 'F': {//查询点巡检任务
				lRc = queryToolInsTaskFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'A': {//新增点巡检计划
				lRc = saveToolInsSettingFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'Z': {//批量新增点巡检计划
				lRc = saveToolInsSettingFunc2(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}

			case 'U': {//更新点巡检任务
				lRc = updateToolInsSettingFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'D': {//删除点巡检任务
				lRc = deleteToolInsSettingFunc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'B':{//新增点检设备和点检任务绑定
				lRc = taskBindTool(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}

			case 'M':{//删除任务和设备的绑定
				lRc = unbindToolTask(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}

			case 'H':{//查询点巡检任务已经绑定的 设备
				lRc = findBindTool(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}

			case 'I':{//查询设备对应的点巡检项目
				lRc = queryInsItemFnc(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}

			case 'R':{//保存点巡检项目记录
				lRc = saveInsRecord(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}

			case 'E':{//任务执行
				lRc = excuteTask(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}

			case 'N':{//任务结单
				lRc = endTask(inTrx, outTrx);
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}

			default: {
				outTrx.setRtn_mesg("Invalide type input: [" + actionType + "]");
				return E_MTPTOOLPM_INVALID_INPUT;
			}
		}
		return lRc;
	}

	//新增点检设备和点检任务绑定 增加点巡检项目
	public long taskBindTool(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
//		inTrx.getIns_task_seq_id();
		String evt_seq_id= inTrx.getEvt_seq_id();//点巡检主键
		String ins_type = inTrx.getIns_type();
		String ins_item_id = inTrx.getIns_item_id();
		List<MtptoolinsIA> iaryAList= inTrx.getIaryA();
		List<Ret_tool_task> retToolTaskList = new ArrayList<>();
		for(int i = 0; i < iaryAList.size(); i++){
			String toolIdFk= iaryAList.get(i).getTool_id_fk();
			Integer dataSeqId = iaryAList.get(i).getData_seq_id();
			Ret_tool_task  ret_tool_task = new Ret_tool_task();
			ret_tool_task.setEvt_seq_id(GUIDGenerator.javaGUID());
			ret_tool_task.setTool_id_fk(toolIdFk);
			ret_tool_task.setData_seq_id(dataSeqId);
			ret_tool_task.setIns_evt_seq_id(evt_seq_id);
			ret_tool_task.setEvt_usr(inTrx.getEvt_usr());
			ret_tool_task.setEvt_timestamp(Util.getCurrentTimestamp());
			retToolTaskList.add(ret_tool_task);

			//先查点巡检基础项目
			String tool_model = "";
			if(!Util.isSpaceCheck(toolIdFk)){
				//根据设备ID获取设备MODEL
				Ret_tool_st tool_st = retToolStDao.get(toolIdFk);
				if(tool_st==null){
					outTrx.setRtn_mesg("找不到设备[" + toolIdFk + "]的信息[RET_TOOL_ST]");
					return E_RET_TOOL_ST + E_READ_NOT_FOUND;
				}if(Util.isSpaceCheck(tool_st.getTool_run_mode())){
					outTrx.setRtn_mesg("设备[" + toolIdFk + "]未维护设备运行模式信息[RET_TOOL_ST]");
					return E_MTPPMITEM_NOT_FOUND_TOOL_MODE_SETTING;
				}else{
					tool_model = tool_st.getTool_run_mode();
				}
			}

			String hql = "From Bis_ins_item where 1=1";
			hql += " and tool_model_fk = '" + tool_model +"'";
			hql += " and ins_type_fk ='" + ins_type +"'";
			List<Bis_ins_item> bisInsItemList = bisInsItemDao.find(hql);
			if(bisInsItemList.size() > 0){
				List<Ret_tool_ins_item> retToolInsItemList = new ArrayList<>();
				for(int j = 0 ; j < bisInsItemList.size() ; j++){
					Bis_ins_item bis_ins_item = bisInsItemList.get(j);
					//保存点巡检项目表
					Ret_tool_ins_item ret_tool_ins_item = new Ret_tool_ins_item();
					ret_tool_ins_item.setEvt_seq_id(GUIDGenerator.javaGUID());
					ret_tool_ins_item.setIns_item_id_fk(ins_item_id);//前台传入
					ret_tool_ins_item.setTool_id_fk(toolIdFk);
					ret_tool_ins_item.setData_seq_id(j);
//					ret_tool_ins_item.setData_group_fk(bis_ins_item.getId().getData_dsc());
					ret_tool_ins_item.setData_dsc(bis_ins_item.getId().getData_dsc());//点巡检项目
					ret_tool_ins_item.setL_spec(bis_ins_item.getL_spec());
					ret_tool_ins_item.setU_spec(bis_ins_item.getU_spec());
					ret_tool_ins_item.setData_typ(bis_ins_item.getData_typ());
					ret_tool_ins_item.setData_cnt(bis_ins_item.getData_cnt());
					ret_tool_ins_item.setEvt_usr(inTrx.getEvt_usr());
					ret_tool_ins_item.setEvt_timestamp(Util.getCurrentTimestamp());
					retToolInsItemList.add(ret_tool_ins_item);
				}
				retToolInsItemDao.saveOrUpdateAll(retToolInsItemList);
			}
		}
		retToolTaskDao.saveOrUpdateAll(retToolTaskList);
		return lRc;
	}


	//查询任务已经绑的设备
	public long findBindTool(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		String evtSeqId = inTrx.getEvt_seq_id();//任务主键

		String hql = "From Ret_tool_task where 1 = 1";
		hql += " and ins_evt_seq_id = '" + evtSeqId +"'";
		hql += " order by data_seq_id asc";
		List<Ret_tool_task> retToolTaskList =retToolTaskDao.find(hql);
		List<MtptoolinsOC> oaryClist = new ArrayList<>();
		if(retToolTaskList.size() > 0){
			for(int i = 0 ; i < retToolTaskList.size(); i++){
				MtptoolinsOC oaryC = new MtptoolinsOC();
				String toolIdFk = retToolTaskList.get(i).getTool_id_fk();
				Bis_tool bis_tool =bisToolDao.get(toolIdFk);
				String toolDsc = bis_tool.getTool_dsc();
				oaryC.setTool_id_fk(toolIdFk);
				oaryC.setTool_dsc(toolDsc);
				oaryC.setEvt_seq_id(retToolTaskList.get(i).getEvt_seq_id());
				oaryClist.add(oaryC);
			}
		}else{
			//TODO
		}
		outTrx.setOaryC(oaryClist);
		return lRc;
	}


	//解绑任务和设备
	public long unbindToolTask(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		String evt_seq_id= inTrx.getEvt_seq_id();//ret_tool_task主键
		String ins_item_id_fk = inTrx.getIns_item_id();
		String toolIdFk = inTrx.getTool_id_fk();
		Ret_tool_task ret_tool_task = retToolTaskDao.get(evt_seq_id);
		if(ret_tool_task != null){
			retToolTaskDao.delete(ret_tool_task);
		}else{
			//todo
		}
		String hql = "From Ret_tool_ins_item where 1 = 1";
		hql += " and ins_item_id_fk ='" + ins_item_id_fk +"'";
		hql += " and tool_id_fk = '" +toolIdFk + "'";
		List<Ret_tool_ins_item>  retToolInsItemList = retToolInsItemDao.find(hql);
		if(retToolInsItemList != null && retToolInsItemList.size() > 0){
			retToolInsItemDao.deleteAll(retToolInsItemList);
		}else{
			//todo
		}

		return lRc;
	}


	//点巡检执行项目查询
	private long queryInsItemFnc(MtptoolinsI inTrx, MtptoolinsO outTrx) {
		long lRc = _NORMAL;
		List<MtptoolinsOB> oaryBList = new ArrayList<MtptoolinsOB>();
		String evtSeqId = inTrx.getEvt_seq_id();//ret_tool_ins_task任务
		String toolIdFk = inTrx.getTool_id_fk();

		String hql = "From Ret_tool_ins_item where 1 = 1";
		hql += " and ins_item_id_fk='" + inTrx.getIns_item_id() + "'";
		hql += " and tool_id_fk='" + inTrx.getTool_id_fk() + "'";
		hql += " order by data_seq_id";
		List<Ret_tool_ins_item> retToolInsItemList = retToolInsItemDao.find(hql);

		String hql2 = "From Ret_tool_ins_list where 1 = 1";
		hql2 += " and ins_evt_seq_id ='" + evtSeqId +"'";
		hql2 += " and tool_id_fk ='" + toolIdFk +"'";
		List<Ret_tool_ins_list>  retToolInsListList = retToolInsListDao.find(hql2);

		if(retToolInsItemList.size() > 0){
			for(Ret_tool_ins_item ret_tool_ins_item : retToolInsItemList){
				for(int i=0; i< ret_tool_ins_item.getData_cnt(); i++){
					MtptoolinsOB oaryB = new MtptoolinsOB();
					String dataDsc = "("+ (i+1) + " of "+ ret_tool_ins_item.getData_cnt() +") " + ret_tool_ins_item.getData_dsc();
					oaryB.setData_dsc(dataDsc);
					oaryB.setU_spec(ret_tool_ins_item.getU_spec());
					oaryB.setL_spec(ret_tool_ins_item.getL_spec());
					oaryBList.add(oaryB);
				}
			}
		}
		if((oaryBList != null && oaryBList.size() > 0) &&
				(retToolInsListList != null && retToolInsListList.size() > 0)){
			for(int i = 0; i< retToolInsListList.size() ;i++){
				for(int j = 0; j < oaryBList.size();j++){
					if(oaryBList.get(j).getData_dsc().equals(retToolInsListList.get(i).getData_dsc())){
						oaryBList.get(j).setData_value(retToolInsListList.get(i).getData_value());
						oaryBList.get(j).setAc_ins_note(retToolInsListList.get(i).getAc_ins_note());
					}
				}
			}
		}

		outTrx.setOaryB(oaryBList);
		return lRc;
	}

	//保存点巡检记录
	public long saveInsRecord(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		String evtSeqId = inTrx.getEvt_seq_id();//ret_tool_ins_task任务
		String toolIdFk = inTrx.getTool_id_fk();
		List<MtptoolinsIB> iaryBList = inTrx.getIaryB();
		if(iaryBList.size() > 0){
			List<Ret_tool_ins_list>  retToolInsListList = new ArrayList<>();
			for(int i = 0 ;i < iaryBList.size(); i++){
				//已经存过的则更新
				String hql = "From Ret_tool_ins_list where 1 = 1";
				hql += " and ins_evt_seq_id ='" + evtSeqId + "'";
				hql += " and tool_id_fk ='" + toolIdFk +"'";
				hql += " and data_dsc ='" + iaryBList.get(i).getData_dsc() +"'";
				List<Ret_tool_ins_list>  retToolInsLists = retToolInsListDao.find(hql);
				if(retToolInsLists != null && retToolInsLists.size() > 0){
					Ret_tool_ins_list retToolInsList = retToolInsLists.get(0);
					retToolInsList.setData_value(iaryBList.get(i).getData_value());
					retToolInsList.setAc_ins_note(iaryBList.get(i).getAc_ins_note());
					retToolInsList.setEvt_usr(inTrx.getEvt_usr());
					retToolInsList.setEvt_timestamp(Util.getCurrentTimestamp());
					retToolInsListList.add(retToolInsList);
				}else{
					Ret_tool_ins_list ret_tool_ins_list = new Ret_tool_ins_list();
					ret_tool_ins_list.setEvt_seq_id(GUIDGenerator.javaGUID());
					ret_tool_ins_list.setIns_evt_seq_id(evtSeqId);
					ret_tool_ins_list.setTool_id_fk(toolIdFk);
//					ret_tool_ins_list.setData_seq_id();
					ret_tool_ins_list.setData_group_fk(iaryBList.get(i).getData_group());
					ret_tool_ins_list.setData_dsc(iaryBList.get(i).getData_dsc());
					ret_tool_ins_list.setData_value(iaryBList.get(i).getData_value());
					if(!Util.isSpaceCheck(iaryBList.get(i).getL_spec())){
						ret_tool_ins_list.setL_spec_fk(Double.valueOf(iaryBList.get(i).getL_spec()));
					}else{
						ret_tool_ins_list.setL_spec_fk(null);
					}
					if(!Util.isSpaceCheck(iaryBList.get(i).getU_spec())){
						ret_tool_ins_list.setU_spec_fk(Double.valueOf(iaryBList.get(i).getU_spec()));
					}else{
						ret_tool_ins_list.setU_spec_fk(null);
					}
					ret_tool_ins_list.setAc_ins_note(iaryBList.get(i).getAc_ins_note());
					ret_tool_ins_list.setEvt_usr(inTrx.getEvt_usr());
					ret_tool_ins_list.setEvt_timestamp(Util.getCurrentTimestamp());
					retToolInsListList.add(ret_tool_ins_list);
				}
			}
			retToolInsListDao.saveOrUpdateAll(retToolInsListList);
		}

		return lRc;
	}

	//执行点巡检任务  WAIT---> INPR
	public long excuteTask(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		String evtSeqId = inTrx.getEvt_seq_id();//点巡检任务主键
		Ret_tool_ins_task retToolInsTask = retToolInsTaskDao.get(evtSeqId);
		retToolInsTask.setIns_task_stat("INPR");
		retToolInsTask.setEvt_cate(EVT_TOOL_INS_BEGIN);
		retToolInsTask.setEvt_usr(inTrx.getEvt_usr());
		retToolInsTask.setEvt_timestamp(Util.getCurrentTimestamp());
		retToolInsTaskDao.save(retToolInsTask);
		return lRc;
	}

	//任务结单  INPR---->COMP
	public long endTask(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		String evtSeqId = inTrx.getEvt_seq_id();//点巡检任务主键
		Ret_tool_ins_task retToolInsTask = retToolInsTaskDao.get(evtSeqId);
		retToolInsTask.setIns_task_stat("COMP");
		retToolInsTask.setEvt_cate(EVT_TOOL_INS_END);
		retToolInsTask.setEvt_usr(inTrx.getEvt_usr());
		retToolInsTask.setEvt_timestamp(Util.getCurrentTimestamp());
		retToolInsTaskDao.save(retToolInsTask);
		return lRc;
	}


	//查询点巡检任务
	public long queryToolInsTaskFunc(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		List<MtptoolinsOA> oaryAList = new ArrayList<MtptoolinsOA>();
		String hql = "From Ret_tool_ins_task where 1 = 1";
		if (!StringUtils.isEmpty(inTrx.getIns_type())) {
			hql += " and ins_type ='" + inTrx.getIns_type() +"'";
		}
		if(!StringUtils.isEmpty(inTrx.getIns_task_stat())){
			hql += " and ins_task_stat = '" + inTrx.getIns_task_stat() + "'";
		}
		if (!StringUtils.isEmpty(inTrx.getFrom_date()) && !StringUtils.isEmpty(inTrx.getTo_date())) {
			hql = hql +" and (from_date between '" +
					inTrx.getFrom_date() +"' and '"+ inTrx.getTo_date()+"')";
		} else if (!StringUtils.isEmpty(inTrx.getFrom_date()) && StringUtils.isEmpty(inTrx.getTo_date())) {
			hql = hql +" and (from_date >='" + inTrx.getFrom_date() +"')";
		} else if (StringUtils.isEmpty(inTrx.getFrom_date()) && !StringUtils.isEmpty(inTrx.getTo_date())) {
			hql = hql +" and (from_date <='" + inTrx.getFrom_date() +")";
		}

		List<Ret_tool_ins_task> retToolInsTaskList = retToolInsTaskDao.find(hql);
		if(retToolInsTaskList.size()>0){
			for(int i=0; i<retToolInsTaskList.size(); i++){
				Ret_tool_ins_task  retToolInsTask = retToolInsTaskList.get(i);
				MtptoolinsOA oaryA = new MtptoolinsOA();
				oaryA.setEvt_seq_id(retToolInsTask.getEvt_seq_id());
				oaryA.setFrom_date(retToolInsTask.getFrom_date().toString());
				oaryA.setTo_date(retToolInsTask.getTo_date().toString());
				oaryA.setIns_type(retToolInsTask.getIns_type());
				oaryA.setIns_route(retToolInsTask.getIns_route());
				oaryA.setIns_task_seq_id(retToolInsTask.getIns_task_seq_id());
				oaryA.setIns_task_stat(retToolInsTask.getIns_task_stat());
				oaryA.setIns_item_id(retToolInsTask.getIns_item_id());
				oaryAList.add(oaryA);
			}
		}
		outTrx.setOaryA(oaryAList);
		return lRc;
	}
	/**
	 * 批量新增点巡检计划
	 * @param inTrx
	 * @param outTrx
	 * @return
	 */
	public long saveToolInsSettingFunc2(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		List<MtptoolinsIA>  iaryAList = inTrx.getIaryA();
		for( MtptoolinsIA iaryA:iaryAList){
		String ins_task_seq_id = GUIDGenerator.javaGUID();
		Ret_tool_ins_task retToolInsTask = new Ret_tool_ins_task();
		retToolInsTask.setEvt_seq_id(GUIDGenerator.javaGUID());
		retToolInsTask.setIns_task_seq_id(ins_task_seq_id);
		retToolInsTask.setIns_item_id(GUIDGenerator.javaGUID());
		retToolInsTask.setIns_type(iaryA.getIns_type());
		retToolInsTask.setIns_route(inTrx.getIns_route());
		retToolInsTask.setFrom_date(Timestamp.valueOf(iaryA.getIns_date_start()));
		retToolInsTask.setTo_date(Timestamp.valueOf(iaryA.getIns_date_end()));
		retToolInsTask.setIns_task_stat(PROC_WAIT);
		retToolInsTask.setEvt_cate(EVT_TOOL_INS_SETTING);
		retToolInsTask.setEvt_usr(inTrx.getEvt_usr());
		retToolInsTask.setEvt_timestamp(Util.getCurrentTimestamp());
		retToolInsTaskDao.save(retToolInsTask);
		outTrx.setIns_task_seq_id(ins_task_seq_id);}
		return lRc;
	}
	/**
	 * 新增点巡检计划
	 * @param inTrx
	 * @param outTrx
	 * @return
	 */
	public long saveToolInsSettingFunc(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		String ins_task_seq_id = GUIDGenerator.javaGUID();
		Ret_tool_ins_task retToolInsTask = new Ret_tool_ins_task();
		retToolInsTask.setEvt_seq_id(GUIDGenerator.javaGUID());
		retToolInsTask.setIns_task_seq_id(ins_task_seq_id);
		retToolInsTask.setIns_item_id(GUIDGenerator.javaGUID());
		retToolInsTask.setIns_type(inTrx.getIns_type());
		retToolInsTask.setIns_route(inTrx.getIns_route());
		retToolInsTask.setFrom_date(Timestamp.valueOf(inTrx.getIns_start_time()));
		retToolInsTask.setTo_date(Timestamp.valueOf(inTrx.getIns_end_time()));
		retToolInsTask.setIns_task_stat(PROC_WAIT);
		retToolInsTask.setEvt_cate(EVT_TOOL_INS_SETTING);
		retToolInsTask.setEvt_usr(inTrx.getEvt_usr());
		retToolInsTask.setEvt_timestamp(Util.getCurrentTimestamp());
		retToolInsTaskDao.save(retToolInsTask);
		outTrx.setIns_task_seq_id(ins_task_seq_id);
		return lRc;
	}

	/**
	 * 更新点巡检任务
	 */
	public long updateToolInsSettingFunc(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		String ins_task_item_id = GUIDGenerator.javaGUID();
		Ret_tool_ins_task retToolInsTask = retToolInsTaskDao.get(inTrx.getEvt_seq_id());
		if(retToolInsTask != null){
			if(retToolInsTask.getIns_task_stat().equals("COMP")){
				outTrx.setRtn_mesg("已完成的任务不能修改");
				return E_MTPTOOLPM_PM_STATUS_INPR;
			}else{
				retToolInsTask.setFrom_date(Timestamp.valueOf(inTrx.getIns_start_time()));
				retToolInsTask.setTo_date(Timestamp.valueOf(inTrx.getIns_end_time()));
				retToolInsTask.setIns_route(inTrx.getIns_route());
				retToolInsTask.setIns_type(inTrx.getIns_type());
				retToolInsTask.setEvt_cate(EVT_TOOL_INS_UPDATE);
				retToolInsTask.setEvt_usr(inTrx.getEvt_usr());
				retToolInsTask.setEvt_timestamp(Util.getCurrentTimestamp());
				retToolInsTask.setIns_task_seq_id(ins_task_item_id);
				retToolInsTaskDao.update(retToolInsTask);
			}

		}else{
			//todo show error
		}
		return lRc;
	}

	/**
	 * 删除点巡检任务
	 */
	public long  deleteToolInsSettingFunc(MtptoolinsI inTrx, MtptoolinsO outTrx){
		long lRc = _NORMAL;
		String hql = "From Ret_tool_ins_task where 1 = 1";
		hql += " and ins_type ='" + inTrx.getIns_type() + "'";
		hql += " and evt_seq_id ='" + inTrx.getEvt_seq_id() +"'";
		List<Ret_tool_ins_task> retToolInsTaskList = retToolInsTaskDao.find(hql);
		if(!retToolInsTaskList.isEmpty()){
			for(Ret_tool_ins_task ret_tool_ins_task : retToolInsTaskList){
				if(PROC_IN_PROCESS.equals(ret_tool_ins_task.getIns_task_stat())){
					outTrx.setRtn_mesg("本次点巡检计划已经开始，无法删除！");
					return E_MTPTOOLPM_PM_STATUS_INPR;
				}else if(PROC_WAIT.equals(ret_tool_ins_task.getIns_task_stat())){//删除WAIT状态的计划
					try {
						ret_tool_ins_task.setEvt_cate(EVT_TOOL_INS_DELETE);
						ret_tool_ins_task.setEvt_usr(inTrx.getEvt_usr());
						ret_tool_ins_task.setEvt_timestamp(Util.getCurrentTimestamp());
						retToolInsTaskDao.delete(ret_tool_ins_task);
					} catch (Exception e) {
						outTrx.setRtn_mesg(e.getCause().getMessage());
						return E_COMMON_ERROR_DELETE;
					}
				}
			}
		}else{
			outTrx.setRtn_mesg("未找到点巡检任务计划，无需删除！");
			return E_MTPTOOLPM_PM_STATUS_INPR;
		}

		return lRc;
	}


//	/**
//	 * 检查当前日期,同一机台，非请一个保养周期（pm_seq_id）是否设定相同保养类型
//	 * @param //d1
//	 * @return
//	 */
//	public boolean isSameSetting(String s_d, String s_t, String s_p, String s_s){
//		boolean is_same = false;
//		String hql_tool_pm = "FROM Ret_tool_pm where 1=1 and tool_id_fk='" + s_t
//				+ "' and pm_type='" + s_p
////				+ "' and from_date=to_date('" + s_d + " 00:00:00','YYYY-MM-DD HH24:mi:ss')";
//				+ "' and from_date='" + s_d + "'";
//		if(!Util.isSpaceCheck(s_s)){
//			hql_tool_pm += " and pm_seq_id<>'" + s_s + "'";
//		}
//
//		List<Ret_tool_pm> tool_pm_list = retToolPmDao.find(hql_tool_pm);
//		if(!tool_pm_list.isEmpty()){
//			is_same = true;
//		}
//		return is_same;
//	}
//
//
//	//判断保养是否过期
//	public static boolean isDealy(String dataString){
//		boolean is_delay = false;
//		Date data = String2Date(dataString);
//		Calendar calendar = Calendar.getInstance(); //得到日历
//		Date d2 = calendar.getTime();
//		if(d2.getTime() > data.getTime()){
//			is_delay = true;
//		}
//		return is_delay;
//	}

}
